//
//  RestNearbyViewController.m
//  FoodFamily
//
//  Created by dlios on 15-6-25.
//  Copyright (c) 2015年 guoBang. All rights reserved.
//

#import "RestNearbyViewController.h"

@interface RestNearbyViewController ()

@property(nonatomic, retain)CLLocationManager *locationManager;
@property(nonatomic, retain)MKMapView *mapView;
@property(nonatomic, retain)CLGeocoder *geocoder;


@end

@implementation RestNearbyViewController

//-(void)dealloc {
//    [_geocoder release];
//    [_mapView release];
//    [_locationManager release];
//
//    [super dealloc];
//
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(laftButtonAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    self.navigationItem.title = @"餐馆详情";
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];

    
    NSString *url = [NSString stringWithFormat:@"http://api.meishixing.com/place/place/placedetail/place_id=%@&session_id=00001227993a5661b25da19c79c2a7c9a6affc",self.place_id];
    
    [AFNGetDataValue getDataAFN:url block:^(NSMutableDictionary *dataDic) {
        
        NSMutableDictionary *dic = [dataDic objectForKey:@"result"];
        self.place_geo_lat = [dic objectForKey:@"place_geo_lat"];
        self.place_geo_lng = [dic objectForKey:@"place_geo_lng"];
        
        
        if (dic.count != 0) {
            
            [self.HUD hide:YES];
            self.restDV = [[RestaruantNearbyDetailView alloc]initWithFrame:CGRectMake(0, 0, WIDTH , HEIGHT- 20 - 44 - 49)];
            self.restDV.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:self.restDV];
            
            self.restDV.contentSize = CGSizeMake(WIDTH, HEIGHT-110);

            self.restDV.dic = dic;

            
            [self getLocation];

        }else if((dic.count == 0)){
            
        [self.HUD hide:YES];
            
            UIAlertView *alView = [[UIAlertView alloc]initWithTitle:nil message:@"未连接到网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alView show];
            
        }
        
    }];

    

    // Do any additional setup after loading the view.
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    [self.navigationController popViewControllerAnimated:YES];

}


-(void)getLocation{

    self.geocoder = [[CLGeocoder alloc] init];
    
    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,0, WIDTH/7 *6, HEIGHT/22 *10)];

    self.mapView.delegate = self;
    self.locationManager = [[CLLocationManager alloc] init];
    if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
        
        [self.locationManager requestWhenInUseAuthorization];
        
    }
    self.mapView.showsUserLocation = YES;
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    self.mapView.mapType = MKMapTypeStandard;
    
    
    self.view1 = [[UIView alloc]initWithFrame:CGRectMake(0,0, WIDTH/7 *6, HEIGHT/22 *10)];
    self.view1.backgroundColor = [UIColor clearColor];
    [self.restDV.mapView  addSubview:self.view1];
    
    [self.view1 addSubview:self.mapView];
    CLLocationCoordinate2D location1=CLLocationCoordinate2DMake([self.place_geo_lat doubleValue], [self.place_geo_lng doubleValue]);
    KCAnnotation *kCAnnotation = [[KCAnnotation alloc] init];
    kCAnnotation.coordinate = location1;
    kCAnnotation.image=[UIImage imageNamed:@"icon_pin_floating.png"];
    self.mapView.showsUserLocation = YES;
    [self.mapView addAnnotation:kCAnnotation];
//    [kCAnnotation release];
    

   
}
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    [self.mapView removeFromSuperview];
    [self.view1 addSubview:mapView];
}


-(void)viewWillDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    self.mapView.showsUserLocation = NO;
    self.mapView.delegate = nil;
    [self.mapView removeFromSuperview];
    self.mapView = nil;
    
    
    
//    [_geocoder release];
//    [_mapView release];
//    [_locationManager release];
}

-(void)laftButtonAction{

    [self.navigationController popViewControllerAnimated:YES];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
